package net.begincode.common;

import java.io.Serializable;

public class ApiResponse<T> implements Serializable {
    //约定大于0位成功，小于0失败
    private static final Integer ERROR_CODE = -1024;
    private static final Integer SUCCESS_CODE = 0;

    private Integer code;
    private String msg;
    private T data;

    public static <T> ApiResponse<T> success(T data) {
        return new ApiResponse<T>(SUCCESS_CODE, "", data);
    }

    public static <T> ApiResponse<T> fail(T data) {
        return new ApiResponse<T>(ERROR_CODE, "", data);
    }

    public static <T> ApiResponse<T> fail(String msg) {
        return new ApiResponse<T>(ERROR_CODE, msg, null);
    }

    public ApiResponse(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public boolean isSuccess() {
        return code >= SUCCESS_CODE;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}

